﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace NX5Web.Utils
{
    public class CaptchaUtil
    {

        public static string CaptchaTextCreator()
        {
            
            string[] C_NUMBERS = "0,1,2,3,4,5,6,7,8,9,10".Split(',');
            Random random = new Random();
            int x = random.Next(10);
            int y = random.Next(10);
            StringBuilder suChinese = new StringBuilder();
            int randomOperands = (int)Math.Round(random.NextDouble() * 2);

            int result;
            switch (randomOperands)
            {
                case 0:
                    result = x * y;
                    suChinese.Append(C_NUMBERS[x]);
                    suChinese.Append("*");
                    suChinese.Append(C_NUMBERS[y]);
                    break;
                case 1:
                    if (!(x == 0) && y % x == 0)
                    {
                        result = y / x;
                        suChinese.Append(C_NUMBERS[y]);
                        suChinese.Append("÷");
                        suChinese.Append(C_NUMBERS[x]);
                    }
                    else
                    {
                        result = x + y;
                        suChinese.Append(C_NUMBERS[x]);
                        suChinese.Append("+");
                        suChinese.Append(C_NUMBERS[y]);
                    }
                    break;
                case 2:
                    if (x >= y)
                    {
                        result = x - y;
                        suChinese.Append(C_NUMBERS[x]);
                        suChinese.Append("-");
                        suChinese.Append(C_NUMBERS[y]);
                    }
                    else
                    {
                        result = y - x;
                        suChinese.Append(C_NUMBERS[y]);
                        suChinese.Append("-");
                        suChinese.Append(C_NUMBERS[x]);
                    }
                    break;
                default:
                    result = x + y;
                    suChinese.Append(C_NUMBERS[x]);
                    suChinese.Append("+");
                    suChinese.Append(C_NUMBERS[y]);
                    break;
            }

            suChinese.Append("=?@").Append(result);
            return suChinese.ToString();
        }
    }
}